<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="法人行"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                          mode="default"
                          :allowClear="true"
                          style="width: 100%"
                          @change="handleInstChange"
                          v-model="queryParams.instId">
                  <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
                </a-select>
<!--                <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                         readonly="readonly"/>-->
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
              <a-form-item
                label="方案确定描述"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input placeholder="方案确定描述 ..." v-model="queryParams.planMapDesc"/>
              </a-form-item>
            </a-col>
          </a-row>


        </div>
        <div style="text-align: center;" v-show="queryParams.instId!=''">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">
      <div class="operator">
        <a-button v-hasPermission="['clearPlanMap:add']" ghost type="primary" @click="addClearPlanMap">新增</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record=>record.settleRuleId"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 1200 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="clearPlanMapOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['clearPlanMap:edit']" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="editClearPlanMap(record)" title="修改"></a-icon>&nbsp;
          <a-icon v-hasPermission="['clearPlanMap:stop']" type="minus-circle" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="stopClearPlanMap(record)" title="停用"></a-icon>&nbsp;
          <a-icon v-hasPermission="['clearPlanMap:use']" type="play-circle" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="useClearPlanMap(record)" title="启用"></a-icon>&nbsp;
          <a-icon v-hasPermission="['clearPlanMap:cancel']" type="poweroff" twoToneColor="#4a9ff5"
                  @click="cancelClearPlanMap(record)" title="注销"></a-icon>&nbsp;
          <a-icon v-hasPermission="['clearPlanMap:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
                  @click="viewClearPlanMap(record)" title="查看"></a-icon>
          <a-badge v-hasNoPermission="['clearPlanMap:edit','clearPlanMap:view']" userStatus="warning" text="无权限"></a-badge>
        </template>

      </a-table>

      <!-- 修改 -->
      <clear-plan-map-operation
        ref="clearPlanMapOperation"
        @close="handleClearPlanMapOperationClose"
        @success="handleClearPlanMapOperationSuccess"
        :clearPlanMapOperationVisiable="clearPlanMapOperation.visiable"
        :instId="this.queryParams.instId">
      </clear-plan-map-operation>

    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate';
  import ClearPlanMapOperation from "./ClearPlanMapOperation";
  import {mapState} from "vuex"

  export default {
    name: "ClearPlanMap",
    components: {ClearPlanMapOperation, RangeDate},
    data() {
      return {
        advanced: false,
        clearPlanMapOperation: {
          visiable: false,
        },
        queryParams: {
          instId: '',
          planMapDesc: '',
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        instIdData: [],
        da: []
      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,

      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '清分清算方案标识',
          dataIndex: 'clearPlanId',
          scopedSlots: {customRender: 'clearPlanId'},
        }, {
          title: '方案确定描述',
          dataIndex: 'planMapDesc',
          scopedSlots: {customRender: 'planMapDesc'},
        }, {
          title: '审核状态',
          dataIndex: 'auditStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "cyan" > 待提交审批 < /a-tag>
              case '01':
                return <a-tag color = "pink" > 新增-审批流程中 < /a-tag>
              case '02':
                return <a-tag color = "pink" > 新增-审批通过 < /a-tag>
              case '03':
                return <a-tag color = "pink" > 新增-审批拒绝 < /a-tag>
              case '10':
                return <a-tag color = "cyan" > 注销-待提交审批 < /a-tag>
              case '11':
                return <a-tag color = "cyan" > 注销-审批流程中 < /a-tag>
              case '12':
                return <a-tag color = "pink" > 注销-审批通过 < /a-tag>
              case '13':
                return <a-tag color = "pink" > 注销-审批拒绝 < /a-tag>
              case '20':
                return <a-tag color = "pink" > 停用-待提交审批 < /a-tag>
              case '21':
                return <a-tag color = "pink" > 停用-审批流程中 < /a-tag>
              case '22':
                return <a-tag color = "cyan" > 停用-审批通过 < /a-tag>
              case '23':
                return <a-tag color = "pink" > 停用-审批拒绝 < /a-tag>
              case '30':
                return <a-tag color = "pink" > 启用-待提交审批 < /a-tag>
              case '31':
                return <a-tag color = "pink" > 启用-审批流程中 < /a-tag>
              case '32':
                return <a-tag color = "pink" > 启用-审批通过 < /a-tag>
              case '33':
                return <a-tag color = "cyan" > 启用-审批拒绝 < /a-tag>
              case '40':
                return <a-tag color = "pink" > 修改-待提交审批 < /a-tag>
              case '41':
                return <a-tag color = "pink" > 修改-审批流程中 < /a-tag>
              case '42':
                return <a-tag color = "pink" > 修改-审批通过 < /a-tag>
              case '43':
                return <a-tag color = "pink" > 修改-审批拒绝 < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '待提交审批', value: '00'},
            {text: '新增-审批流程中', value: '01'},
            {text: '新增-审批通过', value: '02'},
            {text: '新增-审批拒绝', value: '03'},
            {text: '注销-待提交审批', value: '10'},
            {text: '注销-审批流程中', value: '11'},
            {text: '注销-审批通过', value: '12'},
            {text: '注销-审批拒绝', value: '13'},
            {text: '停用-待提交审批', value: '20'},
            {text: '停用-审批流程中', value: '21'},
            {text: '停用-审批通过', value: '22'},
            {text: '停用-审批拒绝', value: '23'},
            {text: '启用-待提交审批', value: '30'},
            {text: '启用-审批流程中', value: '31'},
            {text: '启用-审批通过', value: '32'},
            {text: '启用-审批拒绝', value: '33'},
            {text: '修改-待提交审批', value: '40'},
            {text: '修改-审批流程中', value: '41'},
            {text: '修改-审批通过', value: '42'},
            {text: '修改-审批拒绝', value: '43'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.auditStatus || null,
          onFilter: (value, record) => record.auditStatus.includes(value)
        }, {
          title: '可用状态',
          dataIndex: 'oprStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "pink" > 非正式 < /a-tag>
              case '01':
                return <a-tag color = "cyan" > 正式 < /a-tag>
              case '02':
                return <a-tag color = "pink" > 停用 < /a-tag>
              case '03':
                return <a-tag color = "pink" > 已注销 < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '非正式', value: '00'},
            {text: '正式', value: '01'},
            {text: '停用', value: '02'},
            {text: '已注销', value: '03'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.oprStatus || null,
          onFilter: (value, record) => record.oprStatus.includes(value)
        }, {
          title: '操作',
          dataIndex: 'clearPlanMapOperation',
          fixed: 'right',
          scopedSlots: {customRender: 'clearPlanMapOperation'}
        }]
      }
    },
    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
    },
    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },

      handleInstChange(value) {
        this.queryParams.instId = value;
        if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('/admin/csmc/clearPlanMap', {...params}).then((r) => {
            this.dataSource = r.data.rows;
            // this.search();
          })
        }
      },

      handleClearPlanMapOperationClose() {
        this.clearPlanMapOperation.visiable = false
        this.search()
      },
      handleClearPlanMapOperationSuccess() {
        this.clearPlanMapOperation.visiable = false
        this.$message.success('操作成功')
        this.search()
      },


      viewClearPlanMap(record) {
        record.operation = 'view'
        this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
        this.clearPlanMapOperation.visiable = true
      },

      stopClearPlanMap(record) {
        record.operation = 'stop'
        if (record.auditStatus == "01" || record.auditStatus == "11" || record.auditStatus == "21" ||
          record.auditStatus == "31" || record.auditStatus == "41") {
          this.$message.warning("审核流程中的数据不允许停用");
        } else if(record.oprStatus == "00"){
          this.$message.warning("非正式数据不允许停用");
        } else if (record.oprStatus == "02" || record.oprStatus == "03") {
          this.$message.warning("停用或已注销的数据不允许再停用");
        } else {
          this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
          this.clearPlanMapOperation.visiable = true
        }
      },
      useClearPlanMap(record) {
        record.operation = 'use'
        if (record.auditStatus == "01" || record.auditStatus == "11" || record.auditStatus == "21" ||
          record.auditStatus == "31" || record.auditStatus == "41") {
          this.$message.warning("审批流程中的数据不允许启用");
        } else if(record.oprStatus == "00"){
          this.$message.warning("非正式数据不允许启用");
        } else if (record.oprStatus == "01" || record.oprStatus == "03") {
          this.$message.warning("已启用或已注销的数据不允许再启用");
        } else {
          this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
          this.clearPlanMapOperation.visiable = true
        }
      },

      addClearPlanMap() {
        let record = {}
        record.operation = 'add'
        record.instId = this.queryParams.instId
        record.otherCond1 = '*'
        record.otherCond2 = '*'
        this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
        this.clearPlanMapOperation.visiable = true
      },


      editClearPlanMap(record) {
        record.operation = 'upd'
        if (record.oprStatus == "02" || record.oprStatus == "03") {
          this.$message.warning("停用或已注销的数据不允许修改");
        }else if (record.auditStatus == "01" || record.auditStatus == "11" || record.auditStatus == "21" ||
          record.auditStatus == "31" || record.auditStatus == "41") {
          this.$message.warning("审核流程中的数据不允许修改");
        }else {
          this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
          this.clearPlanMapOperation.visiable = true
        }
      },
      cancelClearPlanMap(record) {
        record.operation = 'cancel'
        if (record.auditStatus == "01" || record.auditStatus == "11" || record.auditStatus == "21" ||
          record.auditStatus == "31" || record.auditStatus == "41") {
          this.$message.warning("审核流程中的数据不允许注销");
        } else if(record.oprStatus != "01" && record.oprStatus != "02"){
          this.$message.warning("非正式、已注销状态不能注销");
        } else if (record.auditStatus == "03") {
          this.$message.warning("注销通过的数据不允许再注销");
        } else {
          this.$refs.clearPlanMapOperation.setClearPlanMapFormValues(record)
          this.clearPlanMapOperation.visiable = true
        }
      },


      search() {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = this.queryParams

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter
        // this.userInfo.visiable = false

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        // this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('/admin/csmc/clearPlanMap', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../../static/less/Common";
</style>

